home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / General / THINK Power 1.0b4 / Extensions Src / Rot13.c < prev    next >
C/C++ Source or Header  |  1993-12-07  |  1KB  |  62 lines

  1. // Rot13.c - Written by Stefan Arentz, August 1993
  2.  
  3. #include "THINK Power Extensions.h"
  4.  
  5. void Rot13(char *textPtr, long textLength);
  6.  
  7. pascal void main(TPCallbackBlock *theCallbacks, WindowPtr theWindow) {
  8.  
  9.     Handle            theText, theResult;
  10.     long            textLength;
  11.     long            selStart, selEnd, firstChar;
  12.         
  13.     theText = theCallbacks->GetWindowContents(theWindow);
  14.     if (theText) {
  15.     
  16.         textLength = GetHandleSize(theText);
  17.         theCallbacks->GetSelection(&selStart, &selEnd, &firstChar);
  18.         
  19.         if (selStart == selEnd) {
  20.             selStart = 0;
  21.             selEnd = textLength;
  22.         }
  23.         
  24.         if (textLength){
  25.  
  26.             theResult = NewHandle(selEnd - selStart);
  27.             if (theResult) {
  28.  
  29.                 BlockMove(*theText + selStart, *theResult, selEnd - selStart);
  30.                 Rot13(*theResult, selEnd - selStart);
  31.                 (void) theCallbacks->Paste(theResult);
  32.                 theCallbacks->SetSelection(selStart, selEnd, selStart);
  33.                 DisposeHandle(theResult);
  34.  
  35.             }
  36.     
  37.         }
  38.  
  39.     }
  40.     
  41. }
  42.  
  43. void Rot13(char *textPtr, long textLength) {
  44.  
  45.     while (textLength--) {
  46.  
  47.         if (*textPtr >= 'A' && *textPtr <= 'Z') {
  48.  
  49.             *textPtr = ((*textPtr - 'A' + 13) % 26) + 'A';
  50.  
  51.         } else if (*textPtr >= 'a' && *textPtr <= 'z') {
  52.  
  53.             *textPtr = ((*textPtr - 'a' + 13) % 26) + 'a';
  54.  
  55.         }
  56.  
  57.         textPtr++;
  58.  
  59.     }
  60.  
  61. }
  62.